package com.mz.ozy.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by administrator on 2019/3/23.
 */
public class TimeUtils {

    /**
     * 得到连个时间相差多少秒
     *
     * @param dbtime1
     * @param dbtime2
     * @return
     */
    public static int getDateDiff(String dbtime1, String dbtime2) {
        int a = 0;
        //String dbtime1 = "2017-02-23";  //第二个日期
        //String dbtime2 = "2017-02-10";  //第一个日期
        //算两个日期间隔多少天
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1;
        try {
            date1 = format.parse(dbtime1);
            Date date2 = format.parse(dbtime2);
            a = (int) ((date1.getTime() - date2.getTime()) / 1000);

            System.out.println(a);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return a;
    }

    public static String getTimeByMinute(String dbtime, int minute) {
        String result = "";
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = format.parse(dbtime);
            long time = date1.getTime() + minute * 60 * 1000;
            Date date2 = new Date(time);
            result = format.format(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * @param endTime 结束时间
     * @param nowTime 当前时间
     * @return 相差秒数，如果>0,才开始倒计时
     */
    public static int getCountDownTime(String endTime, String nowTime) {
        return getDateDiff(endTime, nowTime);
    }


    /**
     * 将秒转时分秒
     *
     * @param time
     * @return
     */
    public static String generateTime(int time) {
        int totalSeconds = time;
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }


    /**
     * 与当前日期相差多少天
     * 当前日期-指定日期  >0则是在过去   <0则是在未来
     *
     * @param date
     * @return
     */
    public static double getGapCount(String date) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //当前时间戳 -> yyyy-MM-dd，再进行日期对比
            Date d1 = new Date(System.currentTimeMillis());
            Date d2 = df.parse(date);
            final double day = ((double) (d1.getTime() - d2.getTime())) / (60 * 60 * 1000 * 24);
            return day;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
